#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;
using pli = pair<ll, int>;

int n, m;
int head[N];
int ver[N], W[N], Next[N], tot;
void add(int u, int v, int w) {
  ++tot;
  ver[tot] = v;
  W[tot] = w;
  Next[tot] = head[u];
  head[u] = tot;
}
ll dis[N];
int pre[N];
void dijkstra() {
  memset(dis, 0x3f, sizeof(dis));
  memset(pre, 0, sizeof(pre));
  priority_queue<pli, vector<pli>, greater<pli>> pq;
  pq.push(pli(0, 1));
  dis[1] = 0;
  pre[1] = 0;
  while (pq.size()) {
    pli now = pq.top();
    pq.pop();
    int u = now.second;
    ll d = now.first;
    if (d > dis[u]) continue;
    for (int i = head[u]; i; i = Next[i]) {
      int v = ver[i];
      int w = W[i];
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        pq.push(pli(dis[v], v));
        pre[v] = u;
      }
    }
  }
}
void solve() {
  cin >> n >> m;
  rep(i, 1, m) {
    int u, v, w;
    cin >> u >> v >> w;
    add(u, v, w), add(v, u, w);
  }
  dijkstra();
  if (pre[n] == 0) {
    cout << -1 << endl;
    return;
  }
  vector<int> ans;
  int v = n;
  while (v) {
    ans.push_back(v);
    v = pre[v];
  }
  reverse(ans.begin(), ans.end());
  for (int x : ans) cout << x << ' ';
  cout << endl;
  return;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  t = 1;
  while (t--) solve();
  return 0;
}